package com.yami.shop.personnft.common.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.Json;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.common.util.StringUtils;
import groovy.lang.Tuple;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * web层通用数据处理
 *
 * @author ruoyi
 */
public class BaseController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String PAGE_NUM = "pageNum"; //当前页数
    private static final String PAGE_SIZE = "pageSize";//页大小
    private static final String CHANNEL_CODE = "channelCode"; //渠道编码
    private static final String CHANNEL_USER = "userId"; //渠道编码
    private static final String CHAIN_USER = "chainCode"; //链编码
    private static final String TYPE = "type"; //创建：1，导入2
    private static final String DATA = "data"; //返回数据接收标志
    private static final String ROWS = "rows"; //返回数据列表
    private static final String TOTAL = "total"; //返回数据总条数

    /**
     *  获取渠道编码
     *
     * @param userId
     * @return
     */
    protected Tuple getChannelInfo(String userId) {

        if(StringUtils.isEmpty(RedisUtil.get(CHANNEL_CODE + "::" + userId))){
            throw new YamiShopBindException("yami.chain.getcheck.fail");
        }
        // 0 - 渠道 1 用户id 2 链 3 类型 （1创建2导入）
        String codeAndUserId = RedisUtil.get(CHANNEL_CODE + "::" + userId);
        String[] userArr = codeAndUserId.split(":");
        return Tuple.tuple(userArr[0], userArr[1],userArr[2],userArr[3]);
    }

    /**
     *  请求参数组装，带渠道编码
     *
     * @param obj
     * @param channelCode
     * @return
     * @throws Exception
     */
    public static Map<String,String> setRequestParame(Object obj, String channelCode, String chainCode) throws Exception {
        return setPageRequestParame(obj, null, null, channelCode,chainCode);
    }

    /**
     *  请求参数组装，带分页参数
     *
     * @param obj
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public static Map<String,String> setPageRequestParame(Object obj, Long pageNum, Long pageSize) throws Exception {
        return setPageRequestParame(obj, pageNum, pageSize, null,null);
    }

    /**
     *  请求参数组装，带分页参数,带渠道编码
     *
     * @param obj
     * @param pageNum
     * @param pageSize
     * @param channelCode
     * @return
     * @throws Exception
     */
    public static Map<String,String> setPageRequestParame(Object obj, Long pageNum, Long pageSize, String channelCode, String chainCode) throws Exception {
        Map<String,String> parames = new HashMap<>();
        if (ObjectUtils.isNotEmpty(obj)) {
            parames = StringUtils.obj2StringMapNotNull(obj);
        }
        if (ObjectUtils.isNotEmpty(pageNum)) {
            parames.put(PAGE_NUM, pageNum+"");
        }
        if (ObjectUtils.isNotEmpty(pageSize)) {
            parames.put(PAGE_SIZE, pageSize+"");
        }
        if (StrUtil.isNotEmpty(channelCode)) {
            parames.put(CHANNEL_CODE, channelCode);
        }
        if (StrUtil.isNotEmpty(chainCode)) {
            parames.put(CHAIN_USER, chainCode);
        }
        return parames;
    }

    /**
     *  分页返回数据，公共处理。只能用于标准的分页数据返回，对手动分页，自定义返回分页数据不使用
     *
     * @param jsonObject
     * @param pageNum
     * @param pageSize
     * @return
     */
    protected IPage<?> page(JSONObject jsonObject, Long pageNum, Long pageSize) {

        IPage<?> res = new PageParam<>();
        res.setCurrent(pageNum);
        res.setSize(pageSize);

        if (jsonObject == null) {
            return res;
        }
        JSONArray rows = jsonObject.getJSONArray(ROWS);
        if (rows != null) {
            List list = JSONArray.parseObject(Json.toJsonString(rows), List.class);
            res.setRecords(list);
        }

        res.setTotal(jsonObject.getInteger(TOTAL));
        return res;
    }

}
